package com.ruoyi.app.controller;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.service.IOperticketService;
import com.ruoyi.app.service.ITaskApplyService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.vo.MemberUserVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentMap;


@RestController
@RequestMapping("/app/taskapply/")
public class ApiTaskApplyController extends BaseController {
    @Autowired
    private ITaskApplyService iTaskApplyService;

    @Anonymous
    /**
     * 获取线路
     */
    @GetMapping("/getCircuit")
    @ResponseBody
    public AjaxResult getCircuit(HttpServletRequest request) {


        Circuit circuit = new Circuit();
        //tempMemberWallet.setMemberId(id);
        List<Circuit> operStaffs = iTaskApplyService.selectCircuitList(circuit);

        return AjaxResult.success("成功",operStaffs);
    }
    @Anonymous
    /**
     * 获取区域
     */
    @GetMapping("/getRegion")
    @ResponseBody
    public AjaxResult getRegion(HttpServletRequest request) {


        Region region = new Region();
        //tempMemberWallet.setMemberId(id);
        List<Region> operStaffs = iTaskApplyService.selectReginList(region);

        return AjaxResult.success("成功",operStaffs);
    }
    @Anonymous
    /**
     * 获取设备
     */
    @GetMapping("/getEquip")
    @ResponseBody
    public AjaxResult getEquip(HttpServletRequest request) {


        String type = request.getParameter("type");
        Equip equip = new Equip();
        equip.setType(Integer.parseInt(type));
        List<Equip> operStaffs = iTaskApplyService.selectEquipList(equip);

        return AjaxResult.success("成功",operStaffs);
    }
    /**
     * 获取设备
     */
    @Anonymous
    @GetMapping("/getTaskApply")
    @ResponseBody
    public AjaxResult getTaskApplyData(HttpServletRequest request) {


        Map<String,Object> data = new HashMap<String,Object>();

        Circuit circuit = new Circuit();
        circuit.setStatus(0);
        List<Circuit> circuits = iTaskApplyService.selectCircuitList(circuit);
        data.put("circuits",circuits);

        Region region = new Region();
        region.setStatus(0);
        List<Region> regions = iTaskApplyService.selectReginList(region);
        data.put("regions",regions);

        Equip equip = new Equip();
        equip.setType(1);
        equip.setStatus(0);
        List<Equip> equips = iTaskApplyService.selectEquipList(equip);
        data.put("Equips",equips);

        Equip equip1 = new Equip();
        equip1.setType(2);
        equip1.setStatus(0);
        List<Equip> equips1 = iTaskApplyService.selectEquipList(equip1);
        data.put("electricEquips",equips1);

        Equip equip2 = new Equip();
        equip2.setType(3);
        equip2.setStatus(0);
        List<Equip> equips2 = iTaskApplyService.selectEquipList(equip2);
        data.put("energyEquips",equips2);

        return AjaxResult.success("成功",data);
    }
    @Anonymous
    @PostMapping("/addTaskApply")
    @Transactional
    @ResponseBody
    public AjaxResult submitTaskApply(@RequestBody TaskApply taskApply) throws Exception {

        TaskApply taskApply1 = new TaskApply();
        taskApply1.setStaffId(taskApply.getStaffId());
        taskApply1.setTaskContent(taskApply.getTaskContent());
        List<TaskApply> taskApplyList = iTaskApplyService.selectTaskApplyList(taskApply1);
        Map<String,Object> data = new HashMap<String,Object>();
        if(taskApplyList.size() > 0){
            return AjaxResult.error("您已提交过该任务");
        }

        taskApply.setStatus(0);
        int res = iTaskApplyService.insertTaskApply(taskApply);
        if (res > 0) {
            data.put("id", taskApply.getId());
            return AjaxResult.success("成功", data);
        } else {
            throw new RuntimeException("提交异常");
        }
    }
    @Anonymous
    @PostMapping("/updateTaskApply")
    @ResponseBody
    public AjaxResult updateTaskApply(@RequestBody TaskApply taskApply) {

        TaskApply taskApply1 = new TaskApply();
        taskApply1.setId(taskApply.getId());

        List<TaskApply> taskApplyList = iTaskApplyService.selectTaskApplyList(taskApply1);

        if(taskApplyList.size() == 0){
            return AjaxResult.error("该任务不存在");
        }
        int res  = iTaskApplyService.insertTaskApply(taskApply);
        if(res == 1){
            return AjaxResult.success("成功");
        }else{
            return AjaxResult.error("失败");
        }
    }
}
